package com.itheima.day07.myCollection.test3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Collection<Student> collection = new ArrayList<>();

        while(true){
            Student stu = new Student();
            String name;
            int age;
            System.out.println("请输入学生姓名：");
            name = sc.nextLine();
            stu.setName(name);

            while (true) {
                System.out.println("请输入年龄：");
                String strAge = sc.nextLine();
                try {
                    age = Integer.parseInt(strAge);
                    stu.setAge(age);
                    collection.add(stu);
                    System.out.println("录入成功");
                    break;
                } catch (NumberFormatException e) {
                    System.out.println("请输入正确的年龄字符，只能存入整数字符\n");
                } catch (AgeOutOfBoundsException e){
                    System.out.println("您年龄不合理，请重新输入\n");
                }
            }

            System.out.println("如想退出请按\"N / n\" ，继续请按任意键");
            String tamp = sc.nextLine();
            if("N".equals(tamp) || "n".equals(tamp)){
                System.out.println("欢迎使用!\n");
                break;
            }

        }
        Iterator<Student> it = collection.iterator();
        while(it.hasNext()){
            Student student = it.next();
            System.out.println(student);
        }

        /*for (Student student : collection) {
            System.out.println(student.toString());
        }*/

    }

}
